<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>实践一下--工作常用小技巧2 | Celts</title><meta name="author" content="PaulGeorge"><meta name="copyright" content="PaulGeorge"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="工作常用小技巧 ( 10条 &#x2F; 篇 )  本篇着重介绍一下工作常用小技巧      写在前面 工作中经常会用到的小技巧，来让我们一起来看看？">
<meta property="og:type" content="article">
<meta property="og:title" content="实践一下--工作常用小技巧2">
<meta property="og:url" content="https://curry3035.gitee.io/posts/32841/index.html">
<meta property="og:site_name" content="Celts">
<meta property="og:description" content="工作常用小技巧 ( 10条 &#x2F; 篇 )  本篇着重介绍一下工作常用小技巧      写在前面 工作中经常会用到的小技巧，来让我们一起来看看？">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://curry3035.gitee.io/img/avatar.jpg">
<meta property="article:published_time" content="2021-04-02T01:00:00.000Z">
<meta property="article:modified_time" content="2022-05-26T18:16:38.833Z">
<meta property="article:author" content="PaulGeorge">
<meta property="article:tag" content="小技巧">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://curry3035.gitee.io/img/avatar.jpg"><link rel="shortcut icon" href="/img/ic.ico"><link rel="canonical" href="https://curry3035.gitee.io/posts/32841/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":false,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: PaulGeorge","link":"链接: ","source":"来源: Celts","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"top-right"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '实践一下--工作常用小技巧2',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-27 02:16:38'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/background.css"><link rel="stylesheet" href="/css/my.css"><meta name="generator" content="Hexo 5.4.2"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">97</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">64</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@img/mig2023/background05.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="Celts"><span class="site-name">Celts</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">实践一下--工作常用小技巧2</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-04-02T01:00:00.000Z" title="发表于 2021-04-02 09:00:00">2021-04-02</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-26T18:16:38.833Z" title="更新于 2022-05-27 02:16:38">2022-05-27</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%B7%A5%E4%BD%9C/">工作</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>6分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="实践一下--工作常用小技巧2"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>工作常用小技巧 ( 10条 / 篇 )</p>
</blockquote>
<p><strong>本篇着重介绍一下工作常用小技巧</strong></p>
<hr>
<img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2021-12-02_10-21-57.png" style="zoom:80%;" />



<p><strong>写在前面</strong></p>
<p><font color=#008000>工作中经常会用到的小技巧，来让我们一起来看看？</font></p>
<span id="more"></span>



<h3 id="Java-中的数据转换问题"><a href="#Java-中的数据转换问题" class="headerlink" title="Java 中的数据转换问题"></a>Java 中的数据转换问题</h3><h4 id="java-中-BigDecimal-和-0-比较"><a href="#java-中-BigDecimal-和-0-比较" class="headerlink" title="java 中 BigDecimal 和 0 比较"></a>java 中 BigDecimal 和 0 比较</h4><p>在 java 中判断一个 BigDecimal 的金额是否大于 0，通常用于两个金额差的比较。</p>
<p><strong>举个荔枝</strong></p>
<p>方法一：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">BigDecimal</span> <span class="variable">num</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BigDecimal</span>(<span class="string">&quot;10&quot;</span>); </span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> num.compareTo(BigDecimal.ZERO); </span><br><span class="line"><span class="keyword">if</span>(i == -<span class="number">1</span>)&#123; </span><br><span class="line">    <span class="comment">//num 小于 0  例如：num = -10.00 </span></span><br><span class="line">&#125; </span><br><span class="line"><span class="keyword">if</span>(i == <span class="number">0</span>)&#123; </span><br><span class="line">    <span class="comment">//num 等于 0，  num = 0.00 </span></span><br><span class="line">&#125; </span><br><span class="line"><span class="keyword">if</span>(i == <span class="number">1</span>)&#123; </span><br><span class="line">    <span class="comment">//num 大于 0  例如：num = 10.00 </span></span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//compareTo(Bigdecimal bigdecimal), 返回0表示等于0，返回1表示大于0，返回-1表示小于0;</span></span><br></pre></td></tr></table></figure>

<p>方法二：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">BigDecimal</span> <span class="variable">num</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BigDecimal</span>(<span class="string">&quot;10&quot;</span>); </span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(num.equals(BigDecimal.ZERO))&#123; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="BigDecimal-的相关用法"><a href="#BigDecimal-的相关用法" class="headerlink" title="BigDecimal 的相关用法"></a>BigDecimal 的相关用法</h4><p><strong>构造器描述</strong></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">BigDecimal(<span class="type">int</span>)       <span class="comment">//创建一个具有参数所指定整数值的对象。</span></span><br><span class="line">BigDecimal(<span class="type">double</span>)    <span class="comment">//创建一个具有参数所指定双精度值的对象。</span></span><br><span class="line">BigDecimal(<span class="type">long</span>)      <span class="comment">//创建一个具有参数所指定长整数值的对象。</span></span><br><span class="line">BigDecimal(String)    <span class="comment">//创建一个具有参数所指定以字符串表示的数值的对象。</span></span><br></pre></td></tr></table></figure>



<p><strong>方法描述</strong></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">add(BigDecimal)        <span class="comment">//BigDecimal对象中的值相加，然后返回这个对象。</span></span><br><span class="line">subtract(BigDecimal)   <span class="comment">//BigDecimal对象中的值相减，然后返回这个对象。</span></span><br><span class="line">multiply(BigDecimal)   <span class="comment">//BigDecimal对象中的值相乘，然后返回这个对象。</span></span><br><span class="line">divide(BigDecimal)     <span class="comment">//BigDecimal对象中的值相除，然后返回这个对象。</span></span><br><span class="line">    </span><br><span class="line">toString()             <span class="comment">//将BigDecimal对象的数值转换成字符串。</span></span><br><span class="line">doubleValue()          <span class="comment">//将BigDecimal对象中的值以双精度数返回。</span></span><br><span class="line">floatValue()           <span class="comment">//将BigDecimal对象中的值以单精度数返回。</span></span><br><span class="line">longValue()            <span class="comment">//将BigDecimal对象中的值以长整数返回。</span></span><br><span class="line">intValue()             <span class="comment">//将BigDecimal对象中的值以整数返回。</span></span><br></pre></td></tr></table></figure>



<p><strong>保留两位小数</strong></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 保留两位小数</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">BigDecimalTest</span><span class="params">()</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="type">double</span> num=<span class="number">13.154215</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//方式一</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">str</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DecimalFormat</span>(<span class="string">&quot;0.00&quot;</span>).format(num);</span><br><span class="line">    System.out.println(str);  <span class="comment">//13.15</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//方式二</span></span><br><span class="line">    <span class="comment">// #.00 表示两位小数 #.0000四位小数</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">str2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DecimalFormat</span>(<span class="string">&quot;#.00&quot;</span>).format(num);</span><br><span class="line">    System.out.println(str2);  <span class="comment">//13.15</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//方式三</span></span><br><span class="line">    <span class="comment">//%.2f %. 表示 小数点前任意位数   2 表示两位小数 格式后的结果为f 表示浮点型</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">result</span> <span class="operator">=</span> String.format(<span class="string">&quot;%.2f&quot;</span>, num);</span><br><span class="line">    System.out.println(result);  <span class="comment">//13.15</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>四舍五入</strong></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 四舍五入</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">BigDecimalTest2</span><span class="params">()</span> &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="type">double</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">3.1415926</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//保留2位小数</span></span><br><span class="line">    <span class="type">double</span> <span class="variable">result</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BigDecimal</span>(num).setScale(<span class="number">2</span>, BigDecimal.ROUND_HALF_UP).doubleValue();</span><br><span class="line">    System.out.println(result);  <span class="comment">//3.14</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>BigDecimal.setScale() //方法用于格式化小数点</li>
</ul>
<ul>
<li>setScale(1) //表示保留一位小数，默认用四舍五入方式 </li>
<li>setScale(1,BigDecimal.ROUND_DOWN) //直接删除多余的小数位，如2.35会变成2.3 </li>
<li>setScale(1,BigDecimal.ROUND_UP) //进位处理，2.35变成2.4 </li>
<li>setScale(1,BigDecimal.ROUND_HALF_UP) //四舍五入，2.35变成2.4</li>
<li>setScaler(1,BigDecimal.ROUND_HALF_DOWN) //四舍五入，2.35变成2.3，如果是5则向下舍</li>
<li>setScaler(1,BigDecimal.ROUND_CEILING) //接近正无穷大的舍入</li>
<li>setScaler(1,BigDecimal.ROUND_FLOOR) //接近负无穷大的舍入，数字&gt;0和ROUND_UP作用一样，数字&lt;0和ROUND_DOWN作用一样</li>
<li>setScaler(1,BigDecimal.ROUND_HALF_EVEN) //向最接近的数字舍入，如果与两个相邻数字的距离相等，则向相邻的偶数舍入。</li>
</ul>
<p><strong>科学计数法</strong></p>
<p>涉及到从Excel导入数据，但如果Excel里单元格类型为数值，但内容数据太长时（如银行账号），导入时，会默认读取为科学计数法。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">BigDecimalTest3</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">result</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BigDecimal</span>(<span class="string">&quot;3.40256010353E11&quot;</span>).toPlainString();</span><br><span class="line">    System.out.println(result);  <span class="comment">//340256010353</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<hr>
<h3 id="MyBatis-Plus-链式调用-Lambda-式"><a href="#MyBatis-Plus-链式调用-Lambda-式" class="headerlink" title="MyBatis-Plus  链式调用 Lambda 式"></a>MyBatis-Plus  链式调用 Lambda 式</h3><p>使用了lambda表达式 可以通过方法引用的方式来使用实体字段名的操作，避免直接写数据库表字段名时的错写名字；</p>
<h4 id="QueryWrapper"><a href="#QueryWrapper" class="headerlink" title="QueryWrapper"></a>QueryWrapper</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 查询条件构造器</span></span><br><span class="line">QueryWrapper&lt;User&gt; wrapper = <span class="keyword">new</span> <span class="title class_">QueryWrapper</span>&lt;&gt;();</span><br><span class="line">wrapper.eq(<span class="string">&quot;user_id&quot;</span>, id);</span><br><span class="line"><span class="comment">// 查询操作</span></span><br><span class="line">List&lt;User&gt; userList = userMapper.selectList(wrapper);</span><br></pre></td></tr></table></figure>

<p>引入lambda, 避免在代码中写类似的于user_id的硬编码</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">QueryWrapper&lt;User&gt; wrapper = <span class="keyword">new</span> <span class="title class_">QueryWrapper</span>&lt;&gt;();</span><br><span class="line">wrapper.lambda().eq(User::getUserId, id);</span><br><span class="line">List&lt;User&gt; userList = userMapper.selectList(wrapper);</span><br></pre></td></tr></table></figure>



<h4 id="LambdaQueryWrapper"><a href="#LambdaQueryWrapper" class="headerlink" title="LambdaQueryWrapper"></a>LambdaQueryWrapper</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">LambdaQueryWrapper&lt;User&gt; wrapper = <span class="keyword">new</span> <span class="title class_">QueryWrapper</span>&lt;User&gt;().lambda();</span><br><span class="line">wrapper.eq(User::getUserId, id);</span><br><span class="line">List&lt;User&gt; userList = userMapper.selectList(wrapper);</span><br></pre></td></tr></table></figure>



<h5 id="链式查询"><a href="#链式查询" class="headerlink" title="链式查询"></a>链式查询</h5><p>查询一组数据, 使用.list()</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">List&lt;User&gt; user = <span class="keyword">new</span> <span class="title class_">LambdaQueryChainWrapper</span>&lt;&gt;(userMapper)</span><br><span class="line">                        .eq(User::getUserId, id)</span><br><span class="line">                        .list();</span><br></pre></td></tr></table></figure>

<p>如果只想查询一条记录，例如通过id查询某条记录的详情，使用.one()</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">User</span> <span class="variable">user</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">LambdaQueryChainWrapper</span>&lt;&gt;(userMapper)</span><br><span class="line">                        .eq(User::getUserId, id)</span><br><span class="line">                        .one();</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="MySql-窗口函数-PARTITION-BY"><a href="#MySql-窗口函数-PARTITION-BY" class="headerlink" title="MySql 窗口函数 PARTITION BY"></a>MySql 窗口函数 PARTITION BY</h3><h4 id="确认MySQL服务器是否支持分区表"><a href="#确认MySQL服务器是否支持分区表" class="headerlink" title="确认MySQL服务器是否支持分区表"></a>确认MySQL服务器是否支持分区表</h4><p><strong>命令：</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">show plugins;</span><br></pre></td></tr></table></figure>



<h4 id="有此插件的话"><a href="#有此插件的话" class="headerlink" title="有此插件的话"></a>有此插件的话</h4><ol>
<li>和其功能很像的group by</li>
</ol>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">group</span> <span class="keyword">by</span>是分组函数，<span class="keyword">partition</span> <span class="keyword">by</span>是分析函数（然后像<span class="built_in">sum</span>()等是聚合函数）；</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>明确常用sql关键字的优先级</strong></li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">from &gt; where &gt; group by &gt; having &gt; order by</span><br></pre></td></tr></table></figure>

<p>而<strong>partition by</strong>应用在以上关键字之后，实际上就是在执行完<strong>select</strong>之后，在所得结果集之上进行<strong>partition</strong></p>
<ol start="3">
<li>但是<strong>partition by</strong>相比较于<strong>group by</strong>，能够在保留全部数据！基础上只对其中某些字段做分组排序，而group by则只保留参与分组的字段和聚合函数的结果。</li>
</ol>
<h4 id="基本语法"><a href="#基本语法" class="headerlink" title="基本语法"></a>基本语法</h4><p>over函数的写法：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">OVER( [ PARTITION BY … ] [ ORDER BY … ] )</span><br><span class="line"></span><br><span class="line">eg:</span><br><span class="line">over（partition by cno order by degree ）</span><br><span class="line"># 先对cno 中相同的进行分区，在cno 中相同的情况下对degree 进行排序</span><br></pre></td></tr></table></figure>

<p><font color=#6AAFE6>OVER()函数不能单独使用，必须跟在 排名函数（ ROW_NUMBER、DENSE_RANK、RANK、NTILE） 或 5种聚合函数（SUM、MAX、MIN、AVG、COUNT）后边。</font></p>
<h4 id="排名开窗函数"><a href="#排名开窗函数" class="headerlink" title="排名开窗函数"></a>排名开窗函数</h4><p>语法结构：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">排名函数() OVER ( [ &lt;partition_by字段&gt; ] &lt;order_by字段&gt; )</span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意：在排名开窗函数中必须使用ORDER BY语句</p>
</blockquote>
<ol>
<li><p><strong>ROW_NUMBER（）</strong>：为每一组的行记录按顺序生成一个唯一的行号。这个用的最多的是不连续的Id上下分页，重新生成id，也就是一行会生成一个连续的id值</p>
<blockquote>
<p>如果是分组，则每个组里面的id是连续的</p>
</blockquote>
</li>
<li><p><strong>RANK（</strong>）也为每一组的行生成一个序号，与ROW_NUMBER()不同的是如果按照ORDER<br>BY的排序，如果有相同的值会生成相同的序号，并且接下来的序号是不连序的。例如两个相同的行生成序号3，那么接下来会生成序号5</p>
</li>
<li><p><strong>DENSE_RANK（）</strong>和<strong>RANK（）</strong>类似，不同的是如果有相同的序号，那么接下来的序号不会间断。也就是说如果两个相同的行生成序号3，那么接下来生成的序号还是4</p>
</li>
<li><p><strong>NTILE</strong> (integer_expression) 按照指定的数目将数据进行分组，并为每一组生成一个序号</p>
</li>
</ol>
<h4 id="聚合开窗函数"><a href="#聚合开窗函数" class="headerlink" title="聚合开窗函数"></a>聚合开窗函数</h4><p>语法结构：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">聚合函数() OVER ( [ partition by 字段] [order by 字段]) ，其中【partition by</span><br><span class="line">字段】和【order by 字段】是可选择的</span><br></pre></td></tr></table></figure>

<ol>
<li>Max() 聚合函数，取最大值。</li>
<li>Sum() 聚合函数，求和，第一行和0相加，第二行和第一行相加，以此类推。</li>
<li>Count() 聚合函数，如果两行完全一样，Count值相同。</li>
</ol>
<blockquote>
<p>另外开窗函数和聚合函数的不同之处是：开窗函数对于每个组返回<strong>多行</strong>，而聚合函数对于每个组只返回<strong>一行</strong></p>
</blockquote>
<hr>
<br/>

<p><strong>未完待续 … …</strong></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://curry3035.gitee.io">PaulGeorge</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://curry3035.gitee.io/posts/32841/">https://curry3035.gitee.io/posts/32841/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://curry3035.gitee.io" target="_blank">Celts</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%B0%8F%E6%8A%80%E5%B7%A7/">小技巧</a></div><div class="post_share"><div class="social-share" data-image="/img/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/13749/" title="Kafka"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Kafka</div></div></a></div><div class="next-post pull-right"><a href="/posts/51404/" title="实践一下--工作常用小技巧"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">实践一下--工作常用小技巧</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/51404/" title="实践一下--工作常用小技巧"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-30</div><div class="title">实践一下--工作常用小技巧</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">PaulGeorge</div><div class="author-info__description">很多事情就像是旅行一样，当你决定要出发的时候，最困难的那部分其实就已经完成了</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">97</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">64</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/PaulGeorge123"><i class="fab fa-github"></i><span>GitHub URL</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/PaulGeorge123" target="_blank" title="Github"><i class="fab fa-github"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#Java-%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE%E8%BD%AC%E6%8D%A2%E9%97%AE%E9%A2%98"><span class="toc-number">1.</span> <span class="toc-text">Java 中的数据转换问题</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#java-%E4%B8%AD-BigDecimal-%E5%92%8C-0-%E6%AF%94%E8%BE%83"><span class="toc-number">1.1.</span> <span class="toc-text">java 中 BigDecimal 和 0 比较</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#BigDecimal-%E7%9A%84%E7%9B%B8%E5%85%B3%E7%94%A8%E6%B3%95"><span class="toc-number">1.2.</span> <span class="toc-text">BigDecimal 的相关用法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#MyBatis-Plus-%E9%93%BE%E5%BC%8F%E8%B0%83%E7%94%A8-Lambda-%E5%BC%8F"><span class="toc-number">2.</span> <span class="toc-text">MyBatis-Plus  链式调用 Lambda 式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#QueryWrapper"><span class="toc-number">2.1.</span> <span class="toc-text">QueryWrapper</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#LambdaQueryWrapper"><span class="toc-number">2.2.</span> <span class="toc-text">LambdaQueryWrapper</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%93%BE%E5%BC%8F%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.2.1.</span> <span class="toc-text">链式查询</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#MySql-%E7%AA%97%E5%8F%A3%E5%87%BD%E6%95%B0-PARTITION-BY"><span class="toc-number">3.</span> <span class="toc-text">MySql 窗口函数 PARTITION BY</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%A1%AE%E8%AE%A4MySQL%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%98%AF%E5%90%A6%E6%94%AF%E6%8C%81%E5%88%86%E5%8C%BA%E8%A1%A8"><span class="toc-number">3.1.</span> <span class="toc-text">确认MySQL服务器是否支持分区表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%89%E6%AD%A4%E6%8F%92%E4%BB%B6%E7%9A%84%E8%AF%9D"><span class="toc-number">3.2.</span> <span class="toc-text">有此插件的话</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-number">3.3.</span> <span class="toc-text">基本语法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8E%92%E5%90%8D%E5%BC%80%E7%AA%97%E5%87%BD%E6%95%B0"><span class="toc-number">3.4.</span> <span class="toc-text">排名开窗函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%81%9A%E5%90%88%E5%BC%80%E7%AA%97%E5%87%BD%E6%95%B0"><span class="toc-number">3.5.</span> <span class="toc-text">聚合开窗函数</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/47231/" title="POI读取Excel问题">POI读取Excel问题</a><time datetime="2023-04-11T01:00:00.000Z" title="发表于 2023-04-11 09:00:00">2023-04-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/8422/" title="Excel大文件的上传">Excel大文件的上传</a><time datetime="2023-04-10T01:00:00.000Z" title="发表于 2023-04-10 09:00:00">2023-04-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/55119/" title="每日一面--Files工具类">每日一面--Files工具类</a><time datetime="2023-01-01T01:00:00.000Z" title="发表于 2023-01-01 09:00:00">2023-01-01</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/34600/" title="面试一下--JUC入门">面试一下--JUC入门</a><time datetime="2022-09-10T01:00:00.000Z" title="发表于 2022-09-10 09:00:00">2022-09-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/16284/" title="每日一面--字符流和字节流">每日一面--字符流和字节流</a><time datetime="2022-07-01T01:00:00.000Z" title="发表于 2022-07-01 09:00:00">2022-07-01</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@img/mig2023/background05.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By PaulGeorge</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></body></html>